/******************************************************************
 *
 *    Java Lib For Android, Powered By GuangZhou WLB.
 *
 *    Copyright (c) 2001-2014 Digital Telemedia Co.,Ltd
 *    http://www.d-telemedia.com/
 *
 *    Package:     com.example.imagepicker.activity
 *
 *    Filename:    PhotoScanActivity.java
 *
 *    Description: TODO(用一句话描述该文件做什么)
 *
 *    Copyright:   Copyright (c) 2001-2014
 *
 *    Company:     Digital Telemedia Co.,Ltd
 *
 *    @author:     WLB
 *
 *    @version:    1.0.0
 *
 *    Create at:   2016年4月20日 下午2:19:54
 *
 *    Revision:
 *
 *    2016年4月20日 下午2:19:54
 *        - first revision
 *
 *****************************************************************/
package com.example.imagepicker.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.imagepicker.fragment.FragmentPhoto;
import com.example.imagepicker.model.Photo;
import com.example.imagepicker.utils.Utility;
import com.tool.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName PhotoScanActivity
 * @author WLB
 * @category 所属功能分类
 * @Date 2016年4月20日 下午2:19:54
 * @Description what
 */
public class PhotoScanActivity extends FragmentActivity implements OnClickListener {

    private String title = "图片预览";
    private List<Photo> paths = new ArrayList<Photo>();
    private ViewPager viewpager;
    private List<FragmentPhoto> fragmentlist = null;
    private Button confirm;// 完成
    private TextView curr_select;
    private ImageView select;// 选择
    
    private Photo currPhoto;
    private int position;
    private int needCount;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photoscan);

        if (getIntent().getExtras() == null) {
            finish();
        }

        paths = (List<Photo>) getIntent().getSerializableExtra("paths");
        if(paths==null){
            Toast.makeText(getApplicationContext(), "缺少必要参数", Toast.LENGTH_SHORT).show();
            return;
        }
        position = getIntent().getIntExtra("position", 0);
        needCount = getIntent().getIntExtra("NeedCount", -1);

        if (paths != null && paths.size() > 0) {

        } else {
            finish();
        }

        initTilte();
        initView();
        initViewpager();

    }

    private void initView() {
        confirm = (Button) findViewById(R.id.btn_common);
        confirm.setOnClickListener(this);
        curr_select = (TextView) findViewById(R.id.curr_select);
        
        if(needCount>0){
            confirm.setText("("+getCount()+"/"+needCount+")完成");  
        }else{
            confirm.setText("("+getCount()+")完成");
        }
        
        int index = position + 1;
        curr_select.setText("("+index+"/" + paths.size() + ")");
        
        findViewById(R.id.ll_phoneselect).setOnClickListener(this);
        select = (ImageView) findViewById(R.id.phoneselect);
    }

    /**
     * @Description (TODO这里用一句话描述这个方法的作用)
     */
    private void initViewpager() {
        fragmentlist = new ArrayList<FragmentPhoto>();
//        FragmentPhoto fragment = null;
        for (int i = 0; i < paths.size(); i++) {
            FragmentPhoto fragment = new FragmentPhoto();
            Bundle bundle=new Bundle();
            bundle.putString("path",paths.get(i).getImgPath());
            fragment.setArguments(bundle);
            fragmentlist.add(fragment);
        }

        viewpager = (ViewPager) findViewById(R.id.viewpager);
        viewpager.setAdapter(new MyViewpagerAdapter(getSupportFragmentManager()));
        viewpager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                int i = arg0 + 1;
                curr_select.setText("(" + i + "/" + paths.size() + ")");
                if (paths.get(arg0).getStatus() == 0) {
                    select.setImageDrawable(getApplicationContext().getResources().getDrawable(
                            R.drawable.checkbox_normal));
                } else {
                    select.setImageDrawable(getApplicationContext().getResources().getDrawable(
                            R.drawable.checkbox_checked));
                }
                currPhoto = paths.get(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        //当为第一个界面是时候不会去调用setOnPageChangeListener
        viewpager.setCurrentItem(position);
        currPhoto = paths.get(position); 

    }

    /**
     * @Description (TODO这里用一句话描述这个方法的作用)
     */
    private void initTilte() {
        TextView head = (TextView) findViewById(R.id.tv_header);
        head.setText(title);

        Button back = (Button) findViewById(R.id.backBtn);
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });
    }

    private class MyViewpagerAdapter extends FragmentPagerAdapter {

        public MyViewpagerAdapter(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int arg0) {
            return fragmentlist.get(arg0);
        }

        @Override
        public int getCount() {
            return fragmentlist != null ? fragmentlist.size() : 0;
        }

    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.btn_common){
            ArrayList<String> list = new ArrayList<String>();
            //刷选出选中的
            for(int i=0;i<paths.size();i++){
                if(paths.get(i).getStatus()==1){
                    list.add(paths.get(i).getImgPath());
                }
            }
            
            Intent intent = new Intent();
            intent.putStringArrayListExtra("paths", list);
            setResult(RESULT_OK,intent);
            finish();
        }else if(v.getId()==R.id.ll_phoneselect){
          //更新有多少选中
            if(needCount>0&&getCount()>=needCount){
                Utility.showToast(this, "选择图片不能超过"+needCount+"张");
            }else{
                if (currPhoto.getStatus() == 0) {
                    currPhoto.setStatus(1);
                    select.setImageDrawable(getApplicationContext().getResources().getDrawable(
                            R.drawable.checkbox_checked));
                } else {
                    currPhoto.setStatus(0);
                    select.setImageDrawable(getApplicationContext().getResources().getDrawable(
                            R.drawable.checkbox_normal));
                }
            }
            if(needCount>0){
                confirm.setText("("+getCount()+"/"+needCount+")完成");
            }else{
                confirm.setText("("+getCount()+")完成"); 
            }
        }

    }
    
    /**
     * @Description (TODO这里用一句话描述这个方法的作用)
     */
    private int getCount() {
        int c = 0;
        for(Photo p:paths){
            if(p.getStatus()==1){
                c++;
            }
        }
        return c;
    }
    
    
}
